/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年10月8日
 */
package com.massyframework.beanskin.connector.spring;

import java.util.Objects;

import org.springframework.beans.factory.config.AbstractFactoryBean;

import com.massyframework.beanskin.assembly.Assembly;
import com.massyframework.beanskin.service.ServiceRepository;
import com.massyframework.beanskin.util.FrameworkUtils;

/**
 * BeanSkinService工厂Bean, 用于关联来自于BeanSkin ServicRepository的服务。
 * @author huangkh
 *
 */
public class BeanSkinServiceFactroyBean<T> extends AbstractFactoryBean<T> {
	
	private Class<T> serviceType;
	private String filterString;
	
	/**
	 * 构造方法
	 */
	public BeanSkinServiceFactroyBean() {
		
	}
	
	/**
	 * 构造方法
	 * @param serviceType {@link Class},服务的类型
	 */
	public BeanSkinServiceFactroyBean(Class<T> serviceType) {
		this.serviceType = Objects.requireNonNull(serviceType, "\"serviceType\" cannot be null.");
	}

	@Override
	public Class<?> getObjectType() {
		return this.serviceType;
	}

	@Override
	protected T createInstance() throws Exception {
		Objects.requireNonNull(this.serviceType, "\"serviceType\" cannot be null.");
		
		Assembly assembly = FrameworkUtils.getKernalAssembly();
		if (assembly != null) {
			ServiceRepository repo = ServiceRepository.retrieveFrom(assembly);
			return this.filterString == null ?
					repo.getService(this.serviceType) :
						repo.getService(this.serviceType, this.filterString);
		}
		return null;
	}
	
	

	/**
	 * 设置服务类型
	 * @param serviceType the serviceType to set
	 */
	public void setServiceType(Class<T> serviceType) {
		this.serviceType = serviceType;
	}

	/**
	 * 服务过滤的字符串
	 * @return the filterString
	 */
	public String getFilterString() {
		return filterString;
	}

	/**
	 * 设置服务过滤所用的字符串
	 * @param filterString the filterString to set
	 */
	public void setFilterString(String filterString) {
		this.filterString = filterString;
	}
}
